<template>
  <el-dialog
    :before-close="handleClose"
    :title="status === 'Add' ? '新增商品' : '编辑商品'"
    :visible.sync="dialogVisible"
    width="50%"
  >
    <el-form
      ref="ruleForm"
      :model="ruleForm"
      :rules="rules"
      class="demo-ruleForm"
      label-width="180px"
    >
      <el-form-item label="等级" prop="grade">
        <el-input v-model="ruleForm.grade" placeholder="请输入等级" style="width: 60%;" type="number"></el-input>
      </el-form-item>
      <el-form-item label="等级名称" prop="name">
        <el-input v-model="ruleForm.name" placeholder="请输入等级名称" style="width: 60%;"></el-input>
      </el-form-item>
      <el-form-item label="经验值" prop="experience">
        <el-input v-model="ruleForm.experience" placeholder="请输入经验值" style="width: 60%;"></el-input>
      </el-form-item>
      <el-form-item label="礼包" prop="reward_id">
        <el-select v-model="ruleForm.reward_id" clearable filterable placeholder="请选择">
          <el-option
            v-for="item in privilegeArr"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="标签图" prop="head_picture">
        <el-upload

          :http-request="customizeUpload"
          :on-success="handleSuccess"
          :show-file-list="false"
          action=""
          class="avatar-uploader"
        >
          <img v-if="ruleForm.head_picture" :src="ruleForm.head_picture" class="avatar"/>
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item label="公屏图" prop="screen_img">
        <el-upload

          :http-request="customizeUpload"
          :on-success="handleSuccess1"
          :show-file-list="false"
          action=""
          class="avatar-uploader"
        >
          <img v-if="ruleForm.screen_img" :src="ruleForm.screen_img" class="avatar"/>
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="handleClose">取消</el-button>
      <el-button
        :loading="submitLoading"
        type="primary"
        @click="submitForm('ruleForm')"
      >提交</el-button>
    </span>
  </el-dialog>
</template>

<script>

import { getGift, gradeConfigAdd, gradeConfigEdit } from '@/api/dailyCheck/detail'

export default {
  name: 'detailAddEdit',
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    },
    // 模态框的状态 Add 为新增  Edit为编辑
    status: {
      type: String,
      default: ['Add']
    },
    detail: {
      type: Object,
      default: {}
    },
    id: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      submitLoading: false,
      privilegeArr: [],
      ruleForm: {
        grade: null, // 等级，必传
        name: null, //等级名称，必传
        experience: null, //等级名称，必传
        head_picture: null, //标签图,必传
        screen_img: null, //公屏图，必传
        reward_id: null   // 礼包id
      },

      rules: {
        grade: [{ required: true, message: '请输入等级', trigger: 'blur' }],
        name: [{ required: true, message: '请输入等级名称', trigger: 'blur' }],
        experience: [{ required: true, message: '请输入经验值', trigger: 'blur' }],
        screen_img: [{ required: true, message: '请上传标签图', trigger: 'blur' }],
        head_picture: [{ required: true, message: '请上传公屏图', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    async getList() {
      const { data } = await getGift()
      this.privilegeArr = data
    },
    handleClose(done) {
      this.$emit('update:dialogVisible', false)
      for (const detailKey in this.ruleForm) {
        this.ruleForm[detailKey] = null
      }
      this.resetForm('ruleForm')
      this.$emit('update:status', 'Add')
      this.$emit('update:detail', {})
    },
    submitForm(formName) {
      let data = {}
      for (const dataKey in this.ruleForm) {
        if (
          dataKey !== 'checkedCities' ||
          !(
            !this.ruleForm[dataKey] &&
            typeof this.ruleForm[dataKey] != 'undefined' &&
            this.ruleForm[dataKey] != 0
          )
        ) {
          data[dataKey] = this.ruleForm[dataKey]
        }
        // 过滤掉为 null
      }
      this.$refs[formName].validate(async valid => {
        if (!valid) {
          this.submitLoading = false
          return false
        }
        this.submitLoading = true
        data.grade_big_id = this.id
        console.log(data)
        let detail
        // 新增活动商品
        if (this.status === 'Add') {
          detail = await gradeConfigAdd(data)
        } else {
          // 编辑
          detail = await gradeConfigEdit(data)
        }
        if (detail.code === 200) {
          this.$message.success(this.status === 'Add' ? '添加成功!' : '编辑成功')
          this.$emit('update:dialogVisible', false)
          this.$emit('updateTable')
          for (const detailKey in this.ruleForm) {
            this.ruleForm[detailKey] = null
          }
          this.$emit('update:status', 'Add')
        }
        this.submitLoading = false
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    handleSuccess() {
      this.ruleForm.head_picture = this.ossLastImgUrl
    },
    handleSuccess1() {
      this.ruleForm.screen_img = this.ossLastImgUrl
    }
  },
  watch: {
    async status(newVal) {
      /* 当 status 为 Edit 则 为编辑框, 需要完成数据的回显   */
      if (newVal === 'Edit') {
        this.ruleForm = Object.assign({}, this.detail)
        this.ruleForm.reward_id = this.detail.reward ? this.detail.reward.id : null
      } else {
        for (const detailKey in this.ruleForm) {
          this.ruleForm[detailKey] = null
        }
      }
    }
  }
}
</script>

<style scoped>
::v-deep.avatar-uploader .el-upload {
  border        : 1px dashed #d9d9d9;
  border-radius : 6px;
  cursor        : pointer;
  position      : relative;
  overflow      : hidden;
}

.avatar-uploader .el-upload:hover {
  border-color : #409eff;
}

.avatar-uploader-icon {
  font-size   : 28px;
  color       : #8c939d;
  width       : 100px;
  height      : 100px;
  line-height : 100px;
  text-align  : center;
}

.avatar {
  width   : 100px;
  height  : 100px;
  display : block;
}

::v-deep .lu-input {
  width : 300px !important;
}

</style>
